package com.etc.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.etc.dao.FxAdminMapper;
import com.etc.entity.FxAdmin;
import com.etc.service.FxAdminService;
import com.github.pagehelper.PageHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FxAdminServiceImpl implements FxAdminService {
    @Autowired
    FxAdminMapper mapper;

    Logger logger = (Logger) LogManager.getLogger(getClass());

    //查询

    /**
     * 用于登入
     */
    @Override
    public List<FxAdmin> select_Login(String name, String password) {
        password = SecureUtil.md5(password);
        List<FxAdmin> list = mapper.select_Login(name, password);

        return list;
    }

    /**
     * 根据 id 查询管理员
     */
    public FxAdmin selectByPrimaryKey(Integer id){
        FxAdmin fxAdmin = mapper.selectByPrimaryKey(id);

        return fxAdmin;
    }

    /**
     * 根据 keywords 分页模糊查询管理员
     */
    public List<FxAdmin> selectAdminByPage(int pageNo, int pageSize, String keywords){
        PageHelper.startPage(pageNo,pageSize);
        List<FxAdmin> list = mapper.selectAdminByPage("%"+ keywords +"%");

        return list;
    };

    //添加

    /**
     * 添加管理员
     * 1 - admin - admin - 21232f297a57a5a743894a0e4a801fc3
     * 2 - root - 123456 - e10adc3949ba59abbe56e057f20f883e
     */
    public boolean insert(FxAdmin fxAdmin){
        String md5Pwd = SecureUtil.md5(fxAdmin.getAdminpwd());
        fxAdmin.setAdminpwd(md5Pwd);
        int result = mapper.insert(fxAdmin);
        return result > 0;
    }

    /**
     *
     */
    public boolean insertSelective(FxAdmin record){
        String md5Pwd = SecureUtil.md5(record.getAdminpwd());
        record.setAdminpwd(md5Pwd);
        int result = mapper.insert(record);
        return result > 0;
    };

    //修改

    /**
     * 选择部分信息修改
     */
    public boolean updateByPrimaryKeySelective(FxAdmin fxAdmin){
        String md5Pwd = SecureUtil.md5(fxAdmin.getAdminpwd());
        fxAdmin.setAdminpwd(md5Pwd);
        int result = mapper.updateByPrimaryKeySelective(fxAdmin);

        return result > 0;
    }

    /**
     * 全部信息修改
     */
    public boolean updateByPrimaryKey(FxAdmin fxAdmin){
        String md5Pwd = SecureUtil.md5(fxAdmin.getAdminpwd());
        fxAdmin.setAdminpwd(md5Pwd);
        int result = mapper.updateByPrimaryKeySelective(fxAdmin);

        return result > 0;
    }

    //删除

    /**
     * 根据 id 删除管理员
     */
    public boolean deleteByPrimaryKey(Integer id){
        int result = mapper.deleteByPrimaryKey(id);

        return result > 0;
    }

}
